{% extends "base.html" %}

{% block main %}
    <div class="container">
        <div class="my-3 p-3 bg-white rounded box-shadow">
         <h6 class="border-bottom border-gray pb-2 mb-3">
             <a href="{{request.user.get_absolute_url }}"><i class="fa fa-reply" aria-hidden="true"></i> 主页</a>-编辑图书</h6>
            <form method="post" enctype="multipart/form-data" action="{% url 'books:edit_book' book.id %}">
                {% csrf_token %}
              <div class="form-group row">
                <label for="id_cover_picture" class="col-sm-1 col-form-label">封面图</label>
                <div class="col-sm-11">
                    <a href="{{ book.cover_picture.url }}">目前:{{ book.cover_picture.url }}</a>
                </div>
              </div>

              <div class="form-group row">
                <label for="id_cover_picture" class="col-sm-1 col-form-label">修改</label>
                <div class="col-sm-10">
                    <input type="file" class="form-control-file {% if 'cover_picture' in invalid_keys %}is-invalid{% endif %}" id="id_cover_picture" name="cover_picture">
                </div>
              </div>

              <div class="form-group row">
                <label for="id_name" class="col-sm-1 col-form-label">书名</label>
                <div class="col-sm-11">
                  <input type="text" class="form-control {% if 'name' in invalid_keys %}is-invalid{% endif %}" id="id_name" required name="name" value="{{ book.name }}">
                </div>
              </div>
              <div class="form-group row">
                <label for="id_desc" class="col-sm-1 col-form-label">简介</label>
                <div class="col-sm-11">
                    <textarea class="form-control {% if 'desc' in invalid_keys %}is-invalid{% endif %}" id="id_desc" rows="5" required name="desc" >{{ book.desc }}</textarea>
                </div>
              </div>

                <div class="form-group row">
                    <label class="col-sm-1" for="id_category">分类</label>
                    <div class="col-sm-2">
                        <select class="form-control" id="id_category" name="category">
                            <option selected>---------</option>
                            {% for category in categories %}
                                <option {% ifequal category book.category %}selected{% endifequal %} value="{{ category.id }}">{{ category.name }}</option>
                            {% endfor %}
                        </select>
                    </div>

                </div>

              <div class="form-group row">
                <div class="col-sm-12 mt-3">
                  <button type="submit" class="btn btn-success">保存修改</button>
                </div>
              </div>
            </form>

            {% if edit_form.errors.items %}
               <div class="alert alert-danger alert-dismissible fade show mt-2" role="alert">
                    {% for key, error in edit_form.errors.items %}
                        {{ error }}
                    {% endfor %}
                  <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                  </button>
                </div>
           {% endif %}
        </div>
    </div>
{% endblock %}

{% block custom_js %}
    <script type="text/javascript">
        {% ifequal 'ko' status %}
            $(function () {
                tips('保存失败~', 'danger');
                return true;
            });
        {% endifequal %}

        {% ifequal 'ok' status %}
            $(function () {
                tips('保存成功~', 'success');
                return true;
            });
        {% endifequal %}
    </script>
{% endblock %}